/*
 * @Descripttion: 
 * @version: 
 * @Author: yuxintao
 * @Date: 2022-04-21 10:59:07
 * @LastEditors: yuxintao
 * @LastEditTime: 2022-04-21 12:30:34
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int sockfd;
char sendbuf[1024];
#define MCAST_ADDR "224.0.2.111"
#define MCAST_PORT 8888
int main() {
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in m_addr;
    m_addr.sin_addr.s_addr = inet_addr(MCAST_ADDR);
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(MCAST_PORT);
    FILE* fp = fopen("./server.txt", "r");
    if (fp == NULL) {
        perror("fopen");
        exit(1);
    }
    printf("start transfer file......\n");
    while (1) {
        memset(sendbuf, 0, sizeof(sendbuf));
        fgets(sendbuf, sizeof(sendbuf), fp);
        if (feof(fp)) {
            memset(sendbuf, 0, sizeof(sendbuf));;
            strcpy(sendbuf, "over");
            sendto(sockfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr*)&m_addr, sizeof(m_addr));
            fclose(fp);
            printf("transfer over\n");
            break;
        }
        sendto(sockfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr*)&m_addr, sizeof(m_addr));
    }
    close(sockfd);
    return 0;
}